<?php
class UtinitiesModel
{
	public static function translateToString($array){
		$return_str = "";
		if($array)
			foreach ($array as $row){
				if($return_str != "")
					$return_str .= ",";
				$return_str .= $row;
			}
		
		return $return_str;
	}
	
	public static function translateToArray($strings){
		if(!$strings)
			return array();
		return explode(",", $strings);
	}
	
	/**
	 * parsing time string
	 * yyyy-mm-dd hh:mm:ss
	 * @param string $time
	 */
	public static function parseTimeString($time){
		$data = array();
		
		$data['year'] = substr($time, 0, 4);
		$data['month'] = substr($time, 5, 2);
		$data['day'] = substr($time, 8, 2);
		$data['hour'] = substr($time, 11, 2);
		$data['minute'] = substr($time, 14, 2);
		$data['second'] = @substr($time, 17, 2);
		
		return $data;
	}
	
	public static function diffMonth($start_time, $end_time){
		$diff= date("m", strtotime($end_time) - strtotime($start_time));
		$diff --;
		
		return $diff;
	}
	
	public static function diffDays($start_time, $end_time){
		$diff= date("d", strtotime($end_time) - strtotime($start_time));
		$diff --;
		
		return $diff;
	}
	
	public static function addMonths($date_str, $val){
    	if($val > 0)
    		$time = strtotime ( "+ $val months", strtotime ($date_str)) ;
    	else 
    		$time = strtotime ( "- " . (-$val) . " months", strtotime ($date_str)) ;
    	$time = date ('Y-m' , $time);
    	
    	return $time;
    }
    
    public static function addMonthsToTime($time_string, $val){
    	if($val > 0)
    		$time = strtotime ( "+ $val months", strtotime ($time_string)) ;
    	else 
    		$time = strtotime ( "- " . (-$val) . " months", strtotime ($time_string)) ;
    	$time = date ('Y-m-d H:i:s' , $time);
    	
    	return $time;
    }
}
?>